package com.gitee.qdbp.jdbc.test.service;

import java.util.Arrays;
import java.util.List;
import com.gitee.qdbp.jdbc.api.SqlBoot;
import com.gitee.qdbp.jdbc.plugins.impl.ColumnNamingConverter;
import com.gitee.qdbp.jdbc.sql.SqlBuffer;
import com.gitee.qdbp.jdbc.test.condition.DeptIsolationBuilder;
import com.gitee.qdbp.jdbc.test.condition.DeptIsolationWhere;
import com.gitee.qdbp.tools.utils.VerifyTools;

/**
 * 实体类工具
 *
 * @author zhaohuihua
 * @version 20201007
 */
public class EntityTools {

    public static String getCurrUserIdFromSession() {
        // 模拟从SESSION中获取当前登录用户ID
        return "U0000001";
    }

    public static List<String> getCurrDeptIdsFromSession() {
        // 模拟从SESSION中获取当前登录用户有权限的部门ID
        return Arrays.asList("D0000001", "D0000005", "D0000012");
    }

    /** 生成部门数据权限查询条件 **/
    public static SqlBuffer buildDeptDataPermission(String deptColumn, SqlBoot sqlBoot) {
        return buildDeptDataPermission(null, null, deptColumn, sqlBoot);
    }

    /** 生成部门数据权限查询条件 **/
    public static SqlBuffer buildDeptDataPermission(String userColumn, String deptColumn, SqlBoot sqlBoot) {
        return buildDeptDataPermission(userColumn, null, deptColumn, sqlBoot);
    }

    /** 生成部门数据权限查询条件 **/
    public static SqlBuffer buildDeptDataPermission(String userColumn, String agentColumn, String deptColumn,
            SqlBoot sqlBoot) {
        if (VerifyTools.isAllBlank(userColumn, agentColumn, deptColumn)) {
            return sqlBoot.newSqlBuffer();
        }
        DeptIsolationWhere condition = new DeptIsolationWhere();
        condition.setDeptField(deptColumn);
        condition.setUserField(userColumn);
        condition.setAgentField(agentColumn);
        DeptIsolationBuilder builder = sqlBoot.plugins().getWhereSqlBuilder(DeptIsolationWhere.class);
        return builder.buildSql(condition, new ColumnNamingConverter(sqlBoot.plugins()), sqlBoot);
    }
}
